package com.management.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.controller.form.FindMonthByConditionForm;
import com.management.controller.form.GetAllProjectPageForm;
import com.management.controller.info.FindAllMonthlyReportInfo;
import com.management.controller.info.GetAllWorktimeLogInfo;
import com.management.controller.info.GetProjectInfo;
import com.management.model.WorktimeLog;
import com.management.dao.WorktimeLogDao;
import com.management.service.WorktimeLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Service
public class WorktimeLogServiceImpl extends ServiceImpl<WorktimeLogDao, WorktimeLog> implements WorktimeLogService {

    @Resource
    private WorktimeLogDao worktimeLogDao;
    @Override
    public Result FindProject() {
        List<WorktimeLog> worktimeLogs = baseMapper.selectList(null);
        List list = CopyUtils.CopyList(worktimeLogs, GetProjectInfo.class);
        return Result.getInstanceList(GetProjectInfo.class).setData(list);
    }

    @Override
    public Result FindAllProjects(GetAllProjectPageForm getAllProjectPageForm) {

        GetAllProjectPageForm projectPageForm =baseMapper.selectWorktime(getAllProjectPageForm);

        Page<WorktimeLog> page = new Page<>();

        List list = CopyUtils.CopyList(projectPageForm.getRecords(), GetAllWorktimeLogInfo.class);

        page.setRecords(list);
        page.setTotal(projectPageForm.getTotal());
        page.setPages(projectPageForm.getPages());
        page.setCurrent(projectPageForm.getCurrent());
        page.setSize(projectPageForm.getSize());
        return Result.getInstance().setData(page);
    }

    @Override
    public Result<?> getAllMonthlyReport(FindMonthByConditionForm findMonthByConditionForm) {
        String stime = findMonthByConditionForm.getStime();
        String ctime = findMonthByConditionForm.getCtime();
        System.out.println(stime);
        System.out.println(ctime);
        List<FindAllMonthlyReportInfo> allMonthlyReportr = worktimeLogDao.getAllMonthlyReportr(stime,ctime);

        System.out.println(allMonthlyReportr);

        //set集合去重
        Set<FindAllMonthlyReportInfo> monthModels = new LinkedHashSet<>();
        for (FindAllMonthlyReportInfo model : allMonthlyReportr) {
            FindAllMonthlyReportInfo model1 = toWorkMonthModel(model.getProjectName(), null, null, null);
            System.out.println(model1);
            FindAllMonthlyReportInfo model2 = this.toWorkMonthModel(model.getProjectName(), model.getAssignedTime(), null, null);
            System.out.println(model2);
            FindAllMonthlyReportInfo model3 = this.toWorkMonthModel(model.getProjectName(), null, model.getAssignee(), model.getActWorkHours());
            System.out.println(model3);
            monthModels.add(model1);
            monthModels.add(model2);
            monthModels.add(model3);
        }
        List<FindAllMonthlyReportInfo> models = new ArrayList<>();
        for (FindAllMonthlyReportInfo monthModel : monthModels) {
            models.add(monthModel);
        }
        for (FindAllMonthlyReportInfo monthModel : models) {
            if(null!=monthModel.getProjectName() && null!=monthModel.getAssignedTime()){
                monthModel.setProjectName(null);
            }
            if(null!=monthModel.getProjectName() && null!=monthModel.getAssignee() && null!=monthModel.getActWorkHours()){
                monthModel.setProjectName(null);
            }
        }


        for (FindAllMonthlyReportInfo model : models) {

            System.out.println(model);
        }


        return Result.getInstanceList(FindAllMonthlyReportInfo.class).setMessage("查询成功").setData(models);
//        return Result.getInstanceList(FindAllMonthlyReportDateInfo.class).setMessage("查询成功").setData(fmrd);
    }

    private static FindAllMonthlyReportInfo toWorkMonthModel(String projectName,String assignedTime, String staffName, BigDecimal actWorkHours){

        return new FindAllMonthlyReportInfo(projectName,assignedTime,staffName,actWorkHours);
    }

}
